package top.soulblack.purchasesale.controller.setting;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.GoodsSeason;
import top.soulblack.purchasesale.service.GoodsSeasonService;

import static top.soulblack.purchasesale.common.CodeMsg.SEASON_DELETE;
import static top.soulblack.purchasesale.common.CodeMsg.SEASON_EDIT_ERROR;
import static top.soulblack.purchasesale.common.CodeMsg.SEASON_EXIST;


/**
 * create by 小川
 */

@Controller
@RequestMapping("/season")
public class GoodsSeasonController {

    @Autowired
    private GoodsSeasonService goodsSeasonService;

    @ResponseBody
    @RequestMapping("/addSeason")
    public Result<Boolean> addSeason(GoodsSeason goodsSeason) {
        // 判断是否存在当前品牌
        if (goodsSeasonService.existSeason(goodsSeason.getName())) {
            return Result.error(SEASON_EXIST);
        }
        goodsSeasonService.addSeason(goodsSeason);
        return Result.success(true);
    }

    @ResponseBody
    @RequestMapping("/editSeason")
    public Result<Boolean> editSeason(GoodsSeason goodsSeason) {
        System.out.println(goodsSeason.getId());
        if (goodsSeasonService.updateSeason(goodsSeason)) {
            return Result.success(true);
        }
        return Result.error(SEASON_DELETE);
    }

    @ResponseBody
    @RequestMapping("/deleteSeason")
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteSeason(int seasonId) {
        System.out.println(seasonId);
        if (goodsSeasonService.deleteSeason(seasonId)) {
            return Result.success(true);
        }
        return Result.error(SEASON_EDIT_ERROR);
    }
}
